
* +++++++++++++++++++++
* CROSSWALK OLD AGS
* CODES TO NEW AGS CODES
* +++++++++++++++++++++

* religion data (from Census 2011) --> this uses old ags
import delimited "${data_raw}/religion/2000S-1022.csv", clear delimiter(";")

keep if _n>=8 & _n <=419
keep v1 v2 
destring v1, replace force
ren (v1 v2) (ags place_name)

* pop data --> uses new ags codes
preserve
	use "${data_derived}/kreis_total_pop_by_age_gender.dta", clear
	keep if year ==2011 
	keep ags place_name 
	ren place_name place_name_pop_data
	tempfile pop_tot_2011
	save `pop_tot_2011'
restore

* now build crosswalk 
* source for this is wikipedia
merge 1:1 ags using `pop_tot_2011'

ren ags ags_old 
gen ags = ags_old 
replace ags = 3159 if inlist(ags_old, 3152, 3156) //Göttingen

replace ags = 13071 if inlist(ags_old, 13002, 13052, 13055, 13056) // Mecklenburgische Seenplatte
replace ags = 13072 if inlist(ags_old, 13051, 13053) // Rostock
replace ags = 13073 if inlist(ags_old, 13005, 13057, 13061) // Vorpommern-Rügen
replace ags = 13074 if inlist(ags_old, 13006, 13058) // Nordwestmecklenburg
replace ags = 13075 if inlist(ags_old, 13001, 13059, 13062) // Vorpommern-Greifswald
replace ags = 13076 if inlist(ags_old, 13054, 13060) // Ludwigslust-Parchim

* clean up and save
replace place_name_pop_data = "zzzz" if _merge ==1 
bys ags (place_name_pop_data) : gen place_name_new = place_name_pop_data[1] 

keep if inlist(_merge, 1, 3) 

keep ags ags_old place_name place_name_new
ren (place_name place_name_new) (place_name_old place_name)

order ags ags_old place_name place_name_old

save "${data_derived}/ags_old_to_new.dta", replace
